<?php
namespace app\admin\model;

use think\Model;

/**
 * @手册管理
 */
class ManualModel extends Model
{
    protected $name = 'manual';
    public function __construct($data = [])
    {
        parent::__construct($data);
    }
    protected $insert = ['createTime']; 
    
    public function ManualGroup()
    {
        return $this->hasOne('ManualGroupModel','id','group');
    }
    
    //保存数据
    public function saveData($data,$where=[]){
        $return = $this::allowField(true)->save($data,$where);
        return $return;
    }
    
    //获取总数量
    public function getCount($where=[['status','neq',-1]]){
        $return = $this::allowField(true)->with(['ManualGroup'])->where($where)->count();
        return $return;
    }
    
    /**
     * @查询列表分页数据
     */
    public function selectDataByPage($page=1,$limit=10,$where=[['status','neq',-1]]){
        $return = $this::field('id,title,status,sort,createTime,group')
                ->with(['ManualGroup'=>function($query) {$query->withField("id,title");}])
                ->where($where)->page($page.','.$limit)
                ->order('sort desc,id desc')
                ->select();
        return $return;
    }
    
    public function selectDataById($id){
        $return = $this::get($id);
        return $return;
    }
    
    //删除数据
    public function del($where){
        $return = $this::save(['status'=>-1,'id'=>$where['id']],$where);
        return $return;
    }
    
    
    //设置字段值
    public function setCreateTimeAttr(){
        return time();
    }
}